package com.guofen.controller.sjq;

import com.guofen.pojo.SjqWaterProcess;
import com.guofen.service.sjq.SjqWaterProcessService;
import com.guofen.util.result.Result;
import com.guofen.util.result.ResultGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@RequestMapping("/sjq/SjqWaterProcess")
@Controller
@Api(tags = "SjqWaterProcessController", description = "水处理生产现场记录表（工艺现场)")
public class SjqWaterProcessController {
    @Autowired
    private SjqWaterProcessService sjqWaterProcessService;

    @ApiOperation("添加记录")
    @RequestMapping(value = "/addForm", method = RequestMethod.POST)
    @ResponseBody
    public Result addForm(@ApiParam(name = "time", value = "时间", required = true) @RequestParam String time,
                          @ApiParam(name = "instantaneous_flow", value = "瞬时流量", required = true) @RequestParam String instantaneous_flow,
                          @ApiParam(name = "flow_meter_reading_1", value = "流量计读数1", required = true) @RequestParam String flow_meter_reading_1,
                          @ApiParam(name = "flow_meter_reading_2", value = "流量计读数2", required = true) @RequestParam String flow_meter_reading_2,
                          @ApiParam(name = "flow_meter_reading_total", value = "总流量计读数", required = true) @RequestParam String flow_meter_reading_total,
                          @ApiParam(name = "o_level", value = "0池液位", required = true) @RequestParam String o_level,
                          /*@ApiParam(name = "water_yield_day", value = "流量计读数1", required = true) @RequestParam String water_yield_day,
                          @ApiParam(name = "water_yield_total", value = "流量计读数2", required = true) @RequestParam String water_yield_total,*/
                          @ApiParam(name = "soil_discharging_start_time", value = "开始排泥时间", required = true) @RequestParam String soil_discharging_start_time,
                          @ApiParam(name = "soil_discharging_end_time", value = "结束排泥时间", required = true) @RequestParam String soil_discharging_end_time,
                          /*@ApiParam(name = "sludge_discharge", value = "排泥量", required = true) @RequestParam String sludge_discharge,
                          @ApiParam(name = "total_processing_capacity", value = "总处理量", required = true) @RequestParam String total_processing_capacity,
                          @ApiParam(name = "return_flow", value = "回流量", required = true) @RequestParam String return_flow,
                          @ApiParam(name = "reflux_ratio", value = "回流比", required = true) @RequestParam String reflux_ratio,*/
                          @ApiParam(name = "alkali_dosage_A", value = "A池加碱量", required = true) @RequestParam String alkali_dosage_A,
                          @ApiParam(name = "alkali_dosage_O", value = "O池加碱量", required = true) @RequestParam String alkali_dosage_O,
                          @ApiParam(name = "remarks", value = "备注", required = false) @RequestParam String remarks){
        double insflow = Double.parseDouble(instantaneous_flow);
        double flow1 = Double.parseDouble(flow_meter_reading_1);
        double flow2 = Double.parseDouble(flow_meter_reading_2);
        double flow_total = Double.parseDouble(flow_meter_reading_total);
        double lecvel0 = Double.parseDouble(o_level);
        double alkaliA = Double.parseDouble(alkali_dosage_A);
        double alkaliO = Double.parseDouble(alkali_dosage_O);

        if (sjqWaterProcessService.addForm(time,insflow, flow1, flow2, flow_total,
                lecvel0, soil_discharging_start_time, soil_discharging_end_time, alkaliA, alkaliO,remarks))
            return ResultGenerator.genSuccessResult();
        else
            return ResultGenerator.genFailResult("保存失败");
    }

    @ApiOperation("查询记录")
    @RequestMapping(value = "/getLast", method = RequestMethod.GET)
    @ResponseBody
    public Result getLast(){
        if (sjqWaterProcessService.findForm() != null)
            return ResultGenerator.genSuccessResult(sjqWaterProcessService.findForm());
        else
            return ResultGenerator.genFailResult("暂无记录");
    }


    @ApiOperation("删除记录")
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    @ResponseBody
    public Result delete(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id) {
        return ResultGenerator.genSuccessResult(sjqWaterProcessService.delete(id));
    }

    @ApiOperation("修改记录")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @ResponseBody
    public Result update(@ApiParam(name="id",value="ID",required=true) @RequestParam Integer id,
                         @ApiParam(name = "time", value = "时间", required = true) @RequestParam String time,
                         @ApiParam(name = "instantaneous_flow", value = "瞬时流量", required = true) @RequestParam String instantaneous_flow,
                         @ApiParam(name = "flow_meter_reading_1", value = "流量计读数1", required = true) @RequestParam String flow_meter_reading_1,
                         @ApiParam(name = "flow_meter_reading_2", value = "流量计读数2", required = true) @RequestParam String flow_meter_reading_2,
                         @ApiParam(name = "flow_meter_reading_total", value = "总流量计读数", required = true) @RequestParam String flow_meter_reading_total,
                         @ApiParam(name = "o_level", value = "0池液位", required = true) @RequestParam String o_level,
                         @ApiParam(name = "water_yield_day", value = "流量计读数1", required = true) @RequestParam String water_yield_day,
                         @ApiParam(name = "water_yield_total", value = "流量计读数2", required = true) @RequestParam String water_yield_total,
                         @ApiParam(name = "soil_discharging_start_time", value = "开始排泥时间", required = true) @RequestParam String soil_discharging_start_time,
                         @ApiParam(name = "soil_discharging_end_time", value = "结束排泥时间", required = true) @RequestParam String soil_discharging_end_time,
                         @ApiParam(name = "sludge_discharge", value = "排泥量", required = true) @RequestParam String sludge_discharge,
                         @ApiParam(name = "total_processing_capacity", value = "总处理量", required = true) @RequestParam String total_processing_capacity,
                         @ApiParam(name = "return_flow", value = "回流量", required = true) @RequestParam String return_flow,
                         @ApiParam(name = "reflux_ratio", value = "回流比", required = true) @RequestParam String reflux_ratio,
                         @ApiParam(name = "alkali_dosage_A", value = "A池加碱量", required = true) @RequestParam String alkali_dosage_A,
                         @ApiParam(name = "alkali_dosage_O", value = "O池加碱量", required = true) @RequestParam String alkali_dosage_O,
                         @ApiParam(name = "remarks", value = "备注", required = false) @RequestParam String remarks) {
        SjqWaterProcess record = new SjqWaterProcess();
        double insflow = Double.parseDouble(instantaneous_flow);
        double flow1 = Double.parseDouble(flow_meter_reading_1);
        double flow2 = Double.parseDouble(flow_meter_reading_2);
        double flow_total = Double.parseDouble(flow_meter_reading_total);
        double lecvel0 = Double.parseDouble(o_level);
        double alkaliA = Double.parseDouble(alkali_dosage_A);
        double alkaliO = Double.parseDouble(alkali_dosage_O);
        double wyd = Double.parseDouble(water_yield_day);
        double wyt = Double.parseDouble(water_yield_total);
        double sd = Double.parseDouble(sludge_discharge);
        double tpc = Double.parseDouble(total_processing_capacity);
        double rf = Double.parseDouble(return_flow);
        double rr = Double.parseDouble(reflux_ratio);

        record.setId(id);
        record.setTime(time);
        record.setInstantaneousFlow(insflow);
        record.setFlowMeterReading1(flow1);
        record.setFlowMeterReading2(flow2);
        record.setFlowMeterReadingTotal(flow_total);
        record.setoLevel(lecvel0);
        record.setSoilDischargingStartTime(soil_discharging_start_time);
        record.setSoilDischargingEndTime(soil_discharging_end_time);
        record.setAlkaliDosageA(alkaliA);
        record.setAlkaliDosageO(alkaliO);
        record.setRemarks(remarks);
        /*计算数据不应该被更新,预留代码，以便后期修改*/
        /*record.setWaterYieldDay(wyd);
        record.setWaterYieldTotal(wyt);
        record.setSludgeDischarge(sd);
        record.setTotalProcessingCapacity(tpc);
        record.setReturnFlow(rf);
        record.setRefluxRatio(rr);*/


        return ResultGenerator.genSuccessResult(sjqWaterProcessService.update(record));
    }

    @ApiOperation("查找记录")
    @RequestMapping(value = "/select",method = RequestMethod.POST)
    @ResponseBody
    public Result select(@ApiParam(name="pageSize",value="页容量",required=true) @RequestParam Integer pageSize,
                         @ApiParam(name="currentPage",value="当前页",required=true) @RequestParam Integer currentPage,
                         @ApiParam(name="startTime",value="开始时间") Long startTime,
                         @ApiParam(name="endTime",value="截止时间") Long endTime) {
        if (startTime == null || endTime == null)
            return ResultGenerator.genSuccessResult(sjqWaterProcessService.get(pageSize, currentPage));
        else
            return ResultGenerator.genSuccessResult(sjqWaterProcessService.getByTime(pageSize, currentPage, startTime, endTime));
    }

    @ApiOperation("更新数据库所有数据（非必要情况不要使用，时间过长）")
    @RequestMapping(value = "/updateAll",method = RequestMethod.GET)
    @ResponseBody
    public Result updateAll() {
        sjqWaterProcessService.updateAll();
        return ResultGenerator.genSuccessResult();
    }
}
